package com.prs.crm.action.hr;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.hr.LeaveType;
import com.prs.crm.service.hr.LeaveTypeService;
import com.prs.crm.service.sys.LogService;

public class LeaveTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private LeaveType leaveType;
	private LeaveTypeService leaveTypeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private Collection<TreeNode> nodes;
	private LogService logService;

	@Action("listLeaveType")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				LeaveType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getLeaveTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editLeaveType")
	public String edit() {
		return SUCCESS;
	}

	@Action(value = "loadLeaveTypeTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadLeaveTypeTree() {
		nodes = new ArrayList<TreeNode>();
		List<LeaveType> lts = this.getLeaveTypeService().getAllLeaveType();
		for (LeaveType lt : lts) {
			TreeNode node = new TreeNode();
			node.setId(lt.getId().toString());
			node.setText(lt.getName());
			nodes.add(node);
		}
		return SUCCESS;
	}

	@Action(value = "saveLeaveType", results = {
			@Result(name = "success", type = "redirect", location = "listLeaveType"),
			@Result(name = "error", type = "chain", location = "editLeaveType") })
	public String save() {
		if (leaveType.getName() == null
				|| leaveType.getName().trim().equals("")) {
			addActionError("名称不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName(LeaveType.class,
				leaveType.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getLeaveTypeService().save(this.leaveType);
		return SUCCESS;
	}

	@Action(value = "removeLeaveType", results = {
			@Result(name = "success", type = "redirect", location = "listLeaveType"),
			@Result(name = "error", type = "chain", location = "listLeaveType") })
	public String remove() {
		if (ids == null) {
			this.addActionError("请选择要删除的请假类型");
			return ERROR;
		}
		for (Integer id : ids) {
			LeaveType type = this.getLeaveTypeService().get(id);
			if (type.getLeaves().size() != 0) {
				this.addActionError("已有请假条引用，不能删除");
				return ERROR;
			}
		}
		this.getLeaveTypeService().batchRemove(ids);
		return SUCCESS;
	}

	public LeaveType getLeaveType() {
		return leaveType;
	}

	public void setLeaveType(LeaveType leaveType) {
		this.leaveType = leaveType;
	}

	public LeaveTypeService getLeaveTypeService() {
		return leaveTypeService;
	}

	public void setLeaveTypeService(LeaveTypeService leaveTypeService) {
		this.leaveTypeService = leaveTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
